package com.me.collector;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.me.collector.Definitions.EntityCategory;
import com.me.collector.Definitions.ItemType;

public class ContactLogic implements ContactListener {

	private Hero hero;
	private FallingItems fallingItems;
	private Ground ground;
	
	public ContactLogic(Hero hero, FallingItems fallingItems, Ground ground)
	{
		this.hero = hero;
		this.fallingItems = fallingItems;
		this.ground = ground;
	}
	
	@Override
	public void beginContact(Contact contact) {
		Body b1 = contact.getFixtureA().getBody();
		Body b2 = contact.getFixtureB().getBody();
		
		short cat1 = contact.getFixtureA().getFilterData().categoryBits;
		short cat2 = contact.getFixtureB().getFilterData().categoryBits;
		
		if(b1.equals(hero.getContainer().getBody()) && cat2 == (short)EntityCategory.Item.getValue()) ContainerItemCollision(b2);
		else if(b2.equals(hero.getContainer().getBody()) && cat1 == (short)EntityCategory.Item.getValue()) ContainerItemCollision(b1);
		else if(cat1 == (short)EntityCategory.Item.getValue() && cat2 == (short)EntityCategory.Ground.getValue()) StaticItemCollision(b1);
		else if(cat1 == (short)EntityCategory.Ground.getValue() && cat2 == (short)EntityCategory.Item.getValue()) StaticItemCollision(b2);
	}
	
	// cBody is the body of the item
	private void ContainerItemCollision(Body cBody)
	{
		FallingObject o = fallingItems.getFallingObjectByBody(cBody);
		if(!o.isRemoveable())
		{
			o.getItem().effect(hero, o, fallingItems);
		}
	}
	
	// cBody is the body the of the item
	private void StaticItemCollision(Body cBody)
	{
		FallingObject o = fallingItems.getFallingObjectByBody(cBody);
		if(!o.isRemoveable())
		{
			o.getItem().impact(hero, o, fallingItems, ground);
		}
	}

	@Override
	public void endContact(Contact contact) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void preSolve(Contact contact, Manifold oldManifold) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postSolve(Contact contact, ContactImpulse impulse) {
		// TODO Auto-generated method stub
		
	}
	
}
